package com.lwq.util;

import com.lwq.annotation.Dict;
import com.lwq.model.system.SysDict;
import com.lwq.model.system.SysDictDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2021/7/19
 */
@Component
public class CacheUtil {

    private static CacheUtil staticInstance = new CacheUtil();

    static public List<SysDict> list;

    @Autowired
    private RedisUtil redis;

    @PostConstruct
    public void init() {
        CacheUtil.list = redis.getCacheList("dict", SysDict.class);
    }

    public static void transfor(List<?> list) {
        if (!list.isEmpty()) {
            for (Object o : list) {
                convertBean(o);
            }
        }
    }

    public static List<SysDictDetail> getDictDetailByCode(String typeCode) {
        List<SysDict> deList = CacheUtil.list.stream().filter(e -> e.getCode().equals(typeCode)).collect(Collectors.toList());
        if (!deList.isEmpty()) {
            return new ArrayList<>(deList.get(0).getDetails());
        } else {
            return new ArrayList<>();
        }
    }

    public static String getValueByCodeAndTitle(String typeCode, String title) {
        List<SysDictDetail> list = CacheUtil.getDictDetailByCode(typeCode);
        if (!list.isEmpty()) {
            return list.stream().filter(e -> e.getName().equals(title)).collect(Collectors.toList()).get(0).getValue();
        } else {
            return null;
        }
    }

    public static String getTitleByCodeAndValue(String typeCode, String value) {
        List<SysDictDetail> list = CacheUtil.getDictDetailByCode(typeCode);
        if (!list.isEmpty()) {
            return list.stream().filter(e -> e.getValue().equals(value)).collect(Collectors.toList()).get(0).getName();
        } else {
            return null;
        }
    }

    public static List<String> dictTimeInterval() {
        List<SysDictDetail> dictDetails = CacheUtil.getDictDetailByCode("timeInterval");
        if (!dictDetails.isEmpty()) {
            return Arrays.asList(dictDetails.get(0).getValue().split(","));
        }
        return null;
    }

    public static void convertBean(Object bean) {
        if (bean == null)
            return;
        Class c = bean.getClass();
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(Dict.class)) {
                field.setAccessible(true);
                Dict dict = field.getAnnotation(Dict.class);
                if (dict != null) {
                    try {
                        String type = dict.code();
                        String title = "";
                        Object fieldValue = field.get(bean);
                        if (fieldValue != null) {
                            title = CacheUtil.getTitleByCodeAndValue(type, fieldValue.toString());
                            String replaceName = field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
                            Method setMethod = c.getMethod("set" + replaceName + "_value", String.class);
                            setMethod.invoke(bean, title);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
